package com.service.sale;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import com.common.DateUtils;
import com.common.spring.SpringMvcUtils;
import com.model.supply.SupplyUserModel;
import com.model.sys.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.excel.EasyExcel;
import com.common.StringUtils;
import com.common.easyui.EasyuiUtils;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;
import com.dao.sale.LablesInfoDao;
import com.model.sale.LablesInfoModel;
import com.common.model.Global;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Service
public class LablesInfoService {
    static final Logger logger = LoggerFactory.getLogger(LablesInfoService.class);

    @Autowired
    LablesInfoDao lablesInfoDao;

    /**
     * 列表查询
     *
     * @param queryMap
     * @return
     */
    public HttpResult selectList(Map<String, Object> queryMap) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return HttpResult.error("当前会话不存在！");
        }
        SupplyUserModel supplyUserModel = SpringMvcUtils.getSupplyUser();
        if (supplyUserModel != null) {
           queryMap.put("supplyAuditId",supplyUserModel.getInfId());
        }
        HttpResult httpResult = new HttpResult();
        EasyuiUtils.addPageNum(queryMap);
        EasyuiUtils.addFilter(queryMap);
        List<LablesInfoModel> list = lablesInfoDao.selectList(queryMap);
        int count = lablesInfoDao.selectListCount(queryMap);
        httpResult.setRows(list);
        httpResult.setTotal(count);
        logger.info("selectList:{}", list);
        return httpResult;
    }

    /**
     * 详情
     *
     * @param
     * @return
     */
    public HttpResult selectInfor(String labId) {
        HttpResult httpResult = new HttpResult();
        LablesInfoModel lablesInfoModel = lablesInfoDao.load(labId);
        httpResult.setData(lablesInfoModel);
        httpResult.setCode(HttpStatus.SUCCESS_CODE);
        return httpResult;
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    public HttpResult save(LablesInfoModel lablesInfoModel) {
        if (lablesInfoModel == null) {
            return HttpResult.error("参数不能为空");
        }

        HttpResult httpResult = new HttpResult();
        int result = 0;
        if (!StringUtils.isNotNull(lablesInfoModel.getLabId())) {
            lablesInfoModel.setLabId(UUID.randomUUID().toString());
            result = lablesInfoDao.insert(lablesInfoModel);
        } else {
            // 提交前先确定是否已提交
            String labId = lablesInfoModel.getLabId();
            LablesInfoModel lable = lablesInfoDao.load(labId);
            if(null ==lable){
                return HttpResult.error("该标签打印申请不存在");
            }
            if((Global.YES+"").equals(lable.getStatus())){
                return HttpResult.error("该条标签打印申请已提交，请勿重复提交。");
            }
            result = lablesInfoDao.update(lablesInfoModel);
        }
        if (result > 0) {
            httpResult.setMsg(lablesInfoModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(lablesInfoModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(lablesInfoModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 客服部审核
     *
     * @param
     * @return
     */
    public HttpResult audit(LablesInfoModel lablesInfoModel) {
        if (lablesInfoModel == null) {
            return HttpResult.error("参数不能为空");
        }
        // 提交前先确定是否已提交
        String labId = lablesInfoModel.getLabId();
        LablesInfoModel lable = lablesInfoDao.load(labId);
        if(null ==lable){
            return HttpResult.error("该标签打印申请不存在");
        }
        if((Global.YES+"").equals(lable.getAuditStatus())){
            return HttpResult.error("该条标签打印申请已审核，无需再次确认。");
        }
        SysUser sysUser = SpringMvcUtils.getSysUser();
        int result = 0;
        HttpResult httpResult = new HttpResult();
        lablesInfoModel.setAuditTime(DateUtils.getCurrentTime());
        lablesInfoModel.setAuditName(sysUser.getUsername());
        lablesInfoModel.setAuditCode(sysUser.getUserid());
        String auditStatus = lablesInfoModel.getAuditStatus() + "";
        if ((Global.YES + "").equals(auditStatus)) {
            lablesInfoModel.setSupplyAuditId("100825");
            lablesInfoModel.setSupplyAuditName("东营区星光图片社");
        }

        result = lablesInfoDao.update(lablesInfoModel);
        if (result > 0) {
            httpResult.setMsg(lablesInfoModel.getAuditStatus().equals(Global.YES + "") ? "审核成功!" : "退回成功!");
            httpResult.setData(lablesInfoModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(lablesInfoModel.getAuditStatus().equals(Global.YES + "") ? "审核失败!" : "退回失败!");
        }
        return httpResult;
    }
    /**
     * 供应商确认
     *
     * @param
     * @return
     */
    public HttpResult supplyAudit(LablesInfoModel lablesInfoModel) {
        if (lablesInfoModel == null) {
            return HttpResult.error("参数不能为空");
        }
        // 提交前先确定是否已提交
        String labId = lablesInfoModel.getLabId();
        LablesInfoModel lable = lablesInfoDao.load(labId);
        if(null ==lable){
            return HttpResult.error("该标签打印申请不存在");
        }
        if((Global.YES+"").equals(lable.getSupplyAuditStatus())){
            return HttpResult.error("该条标签打印申请已确认，无需再次确认。");
        }
        int result = 0;
        HttpResult httpResult = new HttpResult();
        lablesInfoModel.setSupplyAuditTime(DateUtils.getCurrentTime());

        result = lablesInfoDao.update(lablesInfoModel);
        if (result > 0) {
            httpResult.setMsg( "已确认!" );
            httpResult.setData(lablesInfoModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg( "确认失败，请联系管理员!");
        }
        return httpResult;
    }
    /**
     *储运部核对
     *
     * @param
     * @return
     */
    public HttpResult check(LablesInfoModel lablesInfoModel) {
        if (lablesInfoModel == null) {
            return HttpResult.error("参数不能为空");
        }
        // 提交前先确定是否已提交
        String labId = lablesInfoModel.getLabId();
        LablesInfoModel lable = lablesInfoDao.load(labId);
        if(null ==lable){
            return HttpResult.error("该标签打印申请不存在");
        }
        if((Global.YES+"").equals(lable.getCheckStatus())){
            return HttpResult.error("该条标签打印申请已核对，无需再次确认。");
        }
        SysUser sysUser = SpringMvcUtils.getSysUser();
        int result = 0;
        HttpResult httpResult = new HttpResult();
        lablesInfoModel.setCheckTime(DateUtils.getCurrentTime());
        lablesInfoModel.setCheckName(sysUser.getUsername());
        lablesInfoModel.setCheckId(sysUser.getUserid());
        result = lablesInfoDao.update(lablesInfoModel);
        if (result > 0) {
            httpResult.setMsg( "核对完成!" );
            httpResult.setData(lablesInfoModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg( "核对失败，请联系管理员!");
        }
        return httpResult;
    }

    /**
     * 提交
     *
     * @param
     * @return
     */
    public HttpResult commit(LablesInfoModel lablesInfoModel) {
        if (lablesInfoModel == null) {
            return HttpResult.error("参数不能为空");
        }
        lablesInfoModel.setStatus(Global.YES + "");
        HttpResult httpResult = new HttpResult();
        int result = 0;
        result = lablesInfoDao.update(lablesInfoModel);
        if (result > 0) {
            httpResult.setMsg(lablesInfoModel.getStatus().equals(Global.YES + "") ? "提交成功!" : "保存成功!");
            httpResult.setData(lablesInfoModel);
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg(lablesInfoModel.getStatus().equals(Global.YES + "") ? "提交失败!" : "保存失败!");
        }
        return httpResult;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    public HttpResult delete(String labId) {
        if (!StringUtils.isNotNull(labId)) {
            return HttpResult.error("参数不能为空");
        }
        LablesInfoModel model = lablesInfoDao.load(labId);
        if (!model.getStatus().equals(Global.NO + "")) {
            return HttpResult.error("非待提交状态，不可删除！");
        }
        HttpResult httpResult = new HttpResult();
        int result = lablesInfoDao.delete(labId);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 撤回
     *
     * @param
     * @return
     */
    public HttpResult back(String labId) {
        if (!StringUtils.isNotNull(labId)) {
            return HttpResult.error("参数不能为空");
        }
        LablesInfoModel model = lablesInfoDao.load(labId);


        HttpResult httpResult = new HttpResult();
        model.setStatus(Global.NO + "");
        int result = lablesInfoDao.update(model);
        if (result > 0) {
            httpResult.setMsg("删除成功！");
        } else {
            httpResult.setCode(HttpStatus.GLOBAL_EXCEPTION_CODE);
            httpResult.setMsg("不存在此条数据！");
        }
        return httpResult;
    }

    /**
     * 导出
     *
     * @param queryMap
     * @param response
     */
    public void export(Map<String, Object> queryMap, HttpServletResponse response) {
        List<LablesInfoModel> list = lablesInfoDao.selectList(queryMap);
        logger.info("export:{}", list);
        try {
            EasyExcel.write(response.getOutputStream(), LablesInfoModel.class).sheet("模板").doWrite(list);
        } catch (IOException e) {
            logger.error("导出错误:{}", e.getMessage());
        }

    }

    public HttpResult getSalesList(Map params) {
        HttpResult httpResult = new HttpResult();
        if (null != params.get("q")) {
            String name = (String) params.get("q");
            params.put("name", name);
        }
        List<SysUser> sales = lablesInfoDao.getSalesList(params);
        httpResult.setRows(sales);
        return httpResult;
    }
}
